package com.example.tse.rbyair.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.example.tse.rbyair.R;

import org.kymjs.kjframe.utils.PreferenceHelper;

public class CheckUpdateDialog extends DialogFragment {


    private OnOkClickLitener onOkClickLitener;
    private boolean isCheckDialogIsShowed;
    private Activity activity;

    public void setOnOkClickLitener(OnOkClickLitener l) {
        onOkClickLitener = l;
    }


    public interface OnOkClickLitener {
        void OK();

        void Cancel();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
        isCheckDialogIsShowed = PreferenceHelper.readBoolean(this.getActivity(), "Type_Chioce_Update", "isShow");
        if (isCheckDialogIsShowed)
            dismiss();

    }



    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Translucent_NoTitleBar);

        Window window = dialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
//        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
        wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;//DensityUtils
        wlp.dimAmount = 0.3f;// Range is from 1.0 for completely opaque to 0.0 for no* dim
        wlp.gravity = Gravity.CENTER;

        window.setAttributes(wlp);




        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });


        return dialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialog_tips_two_button_view, container);
        Button okSureBtn = (Button) view.findViewById(R.id.ok_sure);
        Button okCancekBtn = (Button) view.findViewById(R.id.ok_cancel);
        okSureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOkClickLitener.OK();
                dismiss();
            }
        });

        okCancekBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOkClickLitener.Cancel();
                dismiss();

            }
        });

        /**
         * 弹出后不再弹出
        * */
        PreferenceHelper.write(activity, "Type_Chioce_Update", "isShow", true);

        return view;
    }



}

